/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.reddys.rad.inventory.service;

import com.reddys.rad.inventory.db.Product;
import com.reddys.rad.inventory.db.ProductCategory;
import com.reddys.rad.inventory.db.PurchaseOrder;
import com.reddys.rad.inventory.db.PurchaseOrderDetails;
import com.reddys.rad.inventory.db.Sale;
import com.reddys.rad.inventory.db.SaleDetails;
import com.reddys.rad.inventory.db.SalesReturn;
import com.reddys.rad.inventory.db.Stock;
import com.reddys.rad.inventory.db.StockDetails;
import com.reddys.rad.inventory.db.Vendor;
import static com.reddys.rad.inventory.service.SalesService.em;
import com.reddys.rad.inventory.utils.EntityManagerImpl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author B V Dinesh
 */
public class SalesReturnService {
    
   static EntityManager em;
    
   public static SalesReturn saveHeader(SalesReturn sale){
        sale = (SalesReturn) EntityManagerImpl.persist(sale);
        return sale;
    }
   public static SaleDetails updateSaleDetails(SaleDetails saleDetails){
        saleDetails = (SaleDetails) EntityManagerImpl.merge(saleDetails);
        return saleDetails;
    }
    
   public static Stock updateStock(Stock stock){
        stock = (Stock) EntityManagerImpl.merge(stock);
        return stock;
    }

    public static ObservableList getSalesList() {
         ObservableList data = FXCollections.observableArrayList();
        em = EntityManagerImpl.getEntityManager();
        Query query = em.createNamedQuery("Sale.findAll");
        List saleList = query.getResultList();
        Iterator iter = saleList.iterator();
        while(iter.hasNext()){
            Sale sale = (Sale) iter.next();
            data.add(sale);
            System.out.println("sale"+sale.getSaleId());
            System.out.println("sale"+data);
        }
        return data;
    }
    
     public static List<SaleDetails> getSaleDetailsList(Sale saleId) {
        
        em = EntityManagerImpl.getEntityManager();
        Query query = em.createNamedQuery("SaleDetails.findBySaleId");
        query.setParameter("saleId", saleId);
        List<SaleDetails> list = (List<SaleDetails>)query.getResultList();
        return list;
    }
     
    public static SaleDetails getSaleDetails(String saleDetailId) {
         //ArrayList<Stock> stockList = new ArrayList<Stock>();
         SaleDetails detail = new SaleDetails();
        em = EntityManagerImpl.getEntityManager();
        Query query = em.createNamedQuery("SaleDetails.findBySaleDetailId");
        query.setParameter("saleDetailId",saleDetailId);
        List list = query.getResultList();
        Iterator iter = list.iterator();
        while(iter.hasNext()){
            detail = (SaleDetails) iter.next();
        }
        return detail;
    }
    
}
